package review.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import review.config.Constants;
import review.config.UserForbiddenException;
import review.config.UserNotFoundException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@Api(value = "用户登录API接口", tags = { "登录操作接口" })
public class LoginAction {

	@ApiOperation(value = "用户登录页面", notes = "")
	@RequestMapping("/login")
	public String login(HttpServletRequest request, Model model) {
		String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
		if (exceptionClassName != null) {

			if (UserNotFoundException.class.getName().equals(exceptionClassName)) {
				model.addAttribute("username_error", "用户不存在");
			}
			if (UserForbiddenException.class.getName().equals(exceptionClassName)) {
				model.addAttribute("locked_error", "该用户已被禁用");
			}
			if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
				model.addAttribute("password_error", "密码错误");
			}
		}
		return "login";
	}

	@ApiOperation(value = "登录成功返回数据", notes = "直接访问登录页面才会返回该接口")
	@ResponseBody
	@RequestMapping("/login_success")
	public Object index() {
		JSONObject object = new JSONObject();
		object.put(Constants.SUCCESS, true);
		return object;
	}

}
